package com.itdianzi.controller.system;

import com.github.pagehelper.PageInfo;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.system.Dept;
import com.itdianzi.service.system.DeptService;
import org.apache.commons.lang3.StringUtils;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {
    @Autowired
    private DeptService deptService;

    @RequestMapping(value = "/list",name = "部门管理查询")
    public String findAll(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer size) {

        PageInfo pageInfo = deptService.findAll(page,size,companyId);
        request.setAttribute("page",pageInfo);
        return "system/dept/dept-list";
    }
    @RequestMapping(value = "/toAdd",name = "部门新建")
    public String toAdd() {

        List<Dept> deptList = deptService.findAll(companyId);

        request.setAttribute("deptList",deptList);
        return "system/dept/dept-add";
    }

    @RequestMapping(value = "/edit",name = "部门添加或修改")
    public String edit(Dept dept) {

        dept.setCompanyId(companyId);
        dept.setCompanyName(companyName);

        if (StringUtils.isBlank(dept.getId())) {

            deptService.edit(dept);
        } else {
            deptService.update(dept);
        }
        return "redirect:/system/dept/list.do";

    }

    @RequestMapping(value = "/toUpdate",name = "跳转修改的页面并获取数据")
    public String toUpdate(String id) {

        Dept dept = deptService.findById(id);
        request.setAttribute("dept",dept);

        List<Dept> deptList = deptService.findAll(companyId);
        request.setAttribute("deptList",deptList);

        return "/system/dept/dept-update";
    }

    @RequestMapping(value = "/delete",name = "部门删除")
    public String delete(String id) {
        deptService.delete(id);
        return "redirect:/system/dept/list.do";
    }

}
